/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.wms.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import com.google.gson.Gson;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.wms.entity.PickDetailed;
import com.jeeplus.modules.wms.service.PickDetailedService;

/**
 * 拣货明细Controller
 * @author 李超辉
 * @version 2017-12-13
 */
@Controller
@RequestMapping(value = "${adminPath}/wms/pickDetailed")
public class PickDetailedController extends BaseController {

	@Autowired
	private PickDetailedService pickDetailedService;
	
	@ModelAttribute
	public PickDetailed get(@RequestParam(required=false) String id) {
		PickDetailed entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pickDetailedService.get(id);
		}
		if (entity == null){
			entity = new PickDetailed();
		}
		return entity;
	}
	
	/**
	 * 拣货明细列表页面
	 */
	@RequiresPermissions("wms:pickDetailed:list")
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/wms/pickDetailedList";
	}

		/**
	 * 拣货明细列表数据
	 */
	@ResponseBody
	@RequiresPermissions("wms:pickDetailed:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(PickDetailed pickDetailed, HttpServletRequest request, HttpServletResponse response, Model model) {
		String pro= UserUtils.getUseroffice();
		if(!pro.equals("总部")){
			pickDetailed.setPro(pro);
		}
		if(pickDetailed.getName()!=null &pickDetailed.getName()!=""){
			String name=pickDetailed.getName();
			String a="";
			String b="";
			String c="";
			String d="";
			String e="";
			String f="";
			String g="";
			String n="";
			boolean bol=true;
			if(bol){
				if(name.indexOf("\\")>=0){
					a=name.substring(0,name.indexOf("\\"));
					name=name.substring(name.indexOf("\\")+1);
				}else{
					n=name;
					bol=false;
				}}
			if(bol){
				if(name.indexOf("\\")>=0){
					b=name.substring(0,name.indexOf("\\"));
					name=name.substring(name.indexOf("\\")+1);
				}else{
					n=a+"\\\\"+name;
					bol=false;
				}}
			if(bol){
				if(name.indexOf("\\")>=0){
					c=name.substring(0,name.indexOf("\\"));
					name=name.substring(name.indexOf("\\")+1);
				}else{
					n=a+"\\\\"+b+"\\\\"+name;
					bol=false;
				}}
			if(bol){
				if(name.indexOf("\\")>=0){
					d=name.substring(0,name.indexOf("\\"));
					name=name.substring(name.indexOf("\\")+1);
				}else{
					n=a+"\\\\"+b+"\\\\"+c+"\\\\"+name;
					bol=false;
				}}
			if(bol){
				if(name.indexOf("\\")>=0){
					e=name.substring(0,name.indexOf("\\"));
					name=name.substring(name.indexOf("\\")+1);
				}else{
					n=a+"\\\\"+b+"\\\\"+c+"\\\\"+d+"\\\\"+name;
					bol=false;
				}}
			if(bol){
				if(name.indexOf("\\")>=0){
					f=name.substring(0,name.indexOf("\\"));
					name=name.substring(name.indexOf("\\")+1);
				}else{
					n=a+"\\\\"+b+"\\\\"+c+"\\\\"+d+"\\\\"+e+"\\\\"+name;
					bol=false;
				}}
			if(bol){
				if(name.indexOf("\\")>=0){
					g=name.substring(0,name.indexOf("\\"));
					name=name.substring(name.indexOf("\\")+1);
				}else{
					n=a+"\\\\"+b+"\\\\"+c+"\\\\"+d+"\\\\"+e+"\\\\"+f+"\\\\"+name;
				}}

			pickDetailed.setName(n);
		}
		Page<PickDetailed> page = pickDetailedService.findPage(new Page<PickDetailed>(request, response), pickDetailed);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑拣货明细表单页面
	 */
	@RequiresPermissions(value={"wms:pickDetailed:view","wms:pickDetailed:add","wms:pickDetailed:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(PickDetailed pickDetailed, Model model) {
		model.addAttribute("pickDetailed", pickDetailed);
		return "modules/wms/pickDetailedForm";
	}

	/**
	 * 保存拣货明细
	 */
	@ResponseBody
	@RequiresPermissions(value={"wms:pickDetailed:add","wms:pickDetailed:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public AjaxJson save(PickDetailed pickDetailed, Model model, RedirectAttributes redirectAttributes) throws Exception{
		AjaxJson j = new AjaxJson();
		if (!beanValidator(model, pickDetailed)){
			j.setSuccess(false);
			j.setMsg("非法参数！");
			return j;
		}
		pickDetailedService.save(pickDetailed);//新建或者编辑保存
		j.setSuccess(true);
		j.setMsg("保存拣货明细成功");
		return j;
	}


	@ResponseBody
	@RequestMapping("findList")
	public String findList(String pid){
		PickDetailed p=new PickDetailed();
		p.setOutbount(pid);
		List<PickDetailed>list=pickDetailedService.findList(p);
		Gson g=new Gson();
		return g.toJson(list).toString();
	}
	/**
	 * 删除拣货明细
	 */
	@ResponseBody
	@RequiresPermissions("wms:pickDetailed:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(PickDetailed pickDetailed, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		pickDetailedService.delete(pickDetailed);
		j.setMsg("删除拣货明细成功");
		return j;
	}
	
	/**
	 * 批量删除拣货明细
	 */
	@ResponseBody
	@RequiresPermissions("wms:pickDetailed:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			pickDetailedService.delete(pickDetailedService.get(id));
		}
		j.setMsg("删除拣货明细成功");
		return j;
	}
	
	/**
	 * 导出excel文件
	 */
	@ResponseBody
	@RequiresPermissions("wms:pickDetailed:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public AjaxJson exportFile(PickDetailed pickDetailed, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
            String fileName = "拣货明细"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<PickDetailed> page = pickDetailedService.findPage(new Page<PickDetailed>(request, response, -1), pickDetailed);
    		new ExportExcel("拣货明细", PickDetailed.class).setDataList(page.getList()).write(response, fileName).dispose();
    		j.setSuccess(true);
    		j.setMsg("导出成功！");
    		return j;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导出拣货明细记录失败！失败信息："+e.getMessage());
		}
			return j;
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("wms:pickDetailed:import")
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<PickDetailed> list = ei.getDataList(PickDetailed.class);
			for (PickDetailed pickDetailed : list){
				try{
					pickDetailedService.save(pickDetailed);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条拣货明细记录。");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条拣货明细记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入拣货明细失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/wms/pickDetailed/?repage";
    }
	
	/**
	 * 下载导入拣货明细数据模板
	 */
	@RequiresPermissions("wms:pickDetailed:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "拣货明细数据导入模板.xlsx";
    		List<PickDetailed> list = Lists.newArrayList(); 
    		new ExportExcel("拣货明细数据", PickDetailed.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/wms/pickDetailed/?repage";
    }

}